#define _CRT_SECURE_NO_WARNINGS 1

#include "game.h"


long long range = RAND_MAX + 1L;
long long offset = 0;

void menu()
{
	printf("******************\n");
	printf("**  0.exit      **\n");
	printf("**  1.start     **\n");
	printf("**  2.setRange  **\n");
	printf("******************\n");
}

void game()
{
	long long guess = 0;
	long long num = rand() % range + offset;
	printf("Game Start!\n");
	printf("The current range is %lld~%lld\n", 0 + offset, range - 1 + offset);

	while (1)
	{
		printf("Guess which number it is?(Input a number)\n");
		scanf("%lld", &guess);
		getchar();
		if (guess < num)
		{
			printf("Is smaller!Try again!\n");

		}
		else if (guess > num)
		{
			printf("Is larger!Try again!\n");
		}
		else
		{
			printf("\nBingo!\nThe Number is %lld.\n", num);
			break;
		}
	}
	printf("Enter any content to continue\n");
	while (getchar() != '\n');
	clean();


}


static void clean()
{
#ifdef _WIN32
	system("cls");
#elif __linux__
	system("clear");
#endif
}

void setRange()
{
	long long start, end;
	printf("The current range is %lld~%lld\n", 0 + offset, range - 1 + offset);
	while (1)
	{
		printf("Please enter the range you want to set(example:2 200), enter 0 to cancel the setting\n");
		scanf("%lld", &start);
		getchar();

		if (start == 0)
		{
			break;
		}
		scanf("%lld", &end);
		getchar();

		if (start >= end)
		{
			printf("error range!Please try again!\n");
			continue;
		}
		else if (start + 10 >= end)
		{
			printf("Range too small!Please try again\n");
			continue;
		}
		else if(end - start > RAND_MAX)
		{
			printf("Range too large!Please try again\n");
			continue;
		}
		else
		{
			offset = start;
			range = end - offset + 1;
			printf("Success,The current range is %lld~%lld\n", 0 + offset, range - 1 + offset);
			break;
		}
	}
	printf("Enter any content to continue\n");
	while (getchar() != '\n');
	clean();
}